library(opendatascot)
library(tidyverse)
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
── Attaching packages ─────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
✓ ggplot2 3.3.3 ✓ purrr 0.3.4
✓ tibble 3.0.6 ✓ dplyr 1.0.2
✓ tidyr 1.1.2 ✓ stringr 1.4.0
✓ readr 1.4.0 ✓ forcats 0.5.0
── Conflicts ────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
ods_all_datasets() %>%
janitor::clean_names() %>%
filter(name == "Life Expectancy") %>%
select(uri)
male_hle_data <- read_csv("data/raw_data/male-healthy-life-expectancy.csv",skip = 10)
── Column specification ────────────────────────────────────────────────────────────────────────────────────────────
cols(
`http://purl.org/linked-data/sdmx/2009/dimension#refArea` = col_character(),
`Reference Area` = col_character(),
`2015-2017` = col_double(),
`2016-2018` = col_double(),
`2017-2019` = col_double()
)
female_hle_data <- read_csv("data/raw_data/female-healthy-life-expectancy.csv", skip = 10)
── Column specification ────────────────────────────────────────────────────────────────────────────────────────────
cols(
`http://purl.org/linked-data/sdmx/2009/dimension#refArea` = col_character(),
`Reference Area` = col_character(),
`2015-2017` = col_double(),
`2016-2018` = col_double(),
`2017-2019` = col_double()
)
hb_shapes <- readOGR(
dsn ="data/simplified_shapefiles/health_boards/NHS_HealthBoards_2019/",
layer = "NHS_HealthBoards_2019",
GDAL1_integer64_policy = TRUE)
Discarded datum OSGB_1936 in CRS definition: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs
OGR data source with driver: ESRI Shapefile
Source: "/Users/user/scot_stats_dashboard/data/simplified_shapefiles/health_boards/NHS_HealthBoards_2019", layer: "NHS_HealthBoards_2019"
with 14 features
It has 4 fields
library(rgeos)
rgeos version: 0.5-5, (SVN revision 640)
GEOS runtime version: 3.8.1-CAPI-1.13.3
Linking to sp version: 1.4-2
Polygon checking: TRUE
hb_simple <- gSimplify(hb_shapes, tol = 0.025, topologyPreserve = TRUE)
56.3950° N, 3.4308° W
uk_shape <- readOGR(
dsn ="../Downloads/scot_stats_dashboard/bdline_essh_gb/Data/GB/",
layer = "county_region",
GDAL1_integer64_policy = TRUE)
Discarded datum OSGB_1936 in CRS definition: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs
OGR data source with driver: ESRI Shapefile
Source: "/Users/user/Downloads/scot_stats_dashboard/bdline_essh_gb/Data/GB", layer: "county_region"
with 26 features
It has 15 fields
Integer64 fields read as doubles: NUMBER NUMBER0 POLYGON_ID UNIT_ID
uk_simple <- gSimplify(uk_shape, tol = 0.025, topologyPreserve = TRUE)
plot(uk_simple)
library(maps)
Attaching package: ‘maps’
The following object is masked from ‘package:purrr’:
map
UK <- map_data("world") %>% filter(region == "UK")
UK
library(broom)
hle_map_data <- spdf_fortified %>%
left_join(hb_lookup, by = c("id" = "id")) %>%
left_join(hle_data, by = c("hb_name" = "reference_area"))
library(plotly)
Registered S3 method overwritten by 'data.table':
method from
print.data.table
Registered S3 method overwritten by 'htmlwidgets':
method from
print.htmlwidget tools:rstudio
Attaching package: ‘plotly’
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout
hle_data <- read_csv("data/clean_data/healthy_life_expectancy.csv")
── Column specification ────────────────────────────────────────────────────────────────────────────────────────────
cols(
area_code = col_character(),
reference_area = col_character(),
reference_period = col_character(),
sex = col_character(),
measurement = col_double()
)
ref_period_var <- "2015-2017"
sex_var <- "male"
user_selections <- list(
reference_period = "2015-2017",
sex = "male"
)
user_selections
$ref_period
[1] "2015-2017"
$sex
[1] "male"
filtered_hle_data <- filter_df(hle_data, user_selections)
my_map
hb_shapes@data <- hb_shapes@data %>%
janitor::clean_names() %>%
left_join(hle_2015_m, by = c("hb_name" = "reference_area"))
labels <- sprintf("<strong>%s</strong><br/>%g years",
hb_shapes$hb_name, hb_shapes$healthy_life_expectancy) %>%
lapply(htmltools::HTML)
# add Health Board polygons, colour based on LE, highlight on hover
my_map %>%
addPolygons(data = hb_shapes, color = "white",
fillColor = ~colorQuantile(
"YlOrRd", (-hb_shapes$healthy_life_expectancy))
(-hb_shapes$healthy_life_expectancy),
weight = 1, fillOpacity = 0.9, label = labels,
highlightOptions = highlightOptions(
color = "black", weight = 2,
opacity = 0.9, bringToFront = TRUE))